package Control;

import java.awt.Color;
import java.awt.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Entity.User;
import Form.Main;

public class CommonControls {
	
	private static User user;
	private static String userType; //Either 'Needy', 'Customer', 'Staff'

	public static User getUser() {
		return user;
	}

	public static void setUser(User user) {
		CommonControls.user = user;
		userType = CommonControls.user.validateUserType();
	}

	public static String getUserType() {
		return userType;
	}

	public static void setUserType(String userType) {
		CommonControls.userType = userType;
	}

	/*Generate random id of length 6 
	 * containing random number of numbers 
	 * and letters at random position
	 */
	public static String generateDefault6DigitCode(){
		String id = "";
		for(int i = 0; i < 6; i++){
			
			Random randomizer = new Random();
			
			boolean even = false;
			if(randomizer.nextInt(2 + 1) % 2 == 0)
				even = true;
			else
				even = false;
			
			if(even){
				int numberId = randomizer.nextInt(9 + 1);
				id += Integer.toString(numberId);
			}
			else{
				char alphaId = (char)(randomizer.nextInt(26) + 'a');
				id += Character.toString(alphaId);
			}
		}
		return id;
	}

	/*Generate random id of length 6 
	 * starting with input character
	 * containing random number of numbers 
	 * and letters at random position
	 */
	public static String generateDefaultType5DigitCode(char type){
		String id = "";
		id += Character.toString(type);
		for(int i = 0; i < 5; i++){
			
			Random randomizer = new Random();
			
			boolean even = false;
			if(randomizer.nextInt(2 + 1) % 2 == 0)
				even = true;
			else
				even = false;
			
			if(even){
				int numberId = randomizer.nextInt(9 + 1);
				id += Integer.toString(numberId);
			}
			else{
				char alphaId = (char)(randomizer.nextInt(26) + 'a');
				id += Character.toString(alphaId);
			}
		}
		return id;
	}
	
	public static Date getCurrentDate(){
		Date currentDate = new Date();
		
		return currentDate;
	}
	
	public static Date addDayToDate(Date date, int days){
		Date newDate = new Date();
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.setTime(date);
		calendarDate.set(Calendar.DATE, calendarDate.get(Calendar.DATE) + days);
		
		newDate.setTime(calendarDate.getTimeInMillis());
		
		return newDate;
	}
	
	public static Date minusDayToDate(Date date, int days){
		Date newDate = new Date();
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.setTime(date);
		calendarDate.set(Calendar.DATE, calendarDate.get(Calendar.DATE) - days);
		
		newDate.setTime(calendarDate.getTimeInMillis());
		
		return newDate;
	}
	
	public static Date addMonthToDate(Date date, int months){
		Date newDate = new Date();
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.setTime(date);
		calendarDate.set(Calendar.MONTH, calendarDate.get(Calendar.MONTH) + months);
		
		newDate.setTime(calendarDate.getTimeInMillis());
		
		return newDate;
	}
	
	public static Date addYearToDate(Date date, int years){
		Date newDate = new Date();
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.setTime(date);
		calendarDate.set(Calendar.YEAR, calendarDate.get(Calendar.YEAR) + years);
		
		newDate.setTime(calendarDate.getTimeInMillis());
		
		return newDate;
	}
	
	public static Date minusYearToDate(Date date, int years){
		Date newDate = new Date();
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.setTime(date);
		calendarDate.set(Calendar.YEAR, calendarDate.get(Calendar.YEAR) - years);
		
		newDate.setTime(calendarDate.getTimeInMillis());
		
		return newDate;
	}
	
	public static void togglePanelModality(JPanel panel){
		if(panel.isEnabled()){
			panel.setEnabled(false);
			Component[] components = panel.getComponents();
			for(int i = 0; i < components.length; i++){
				components[i].setEnabled(false);
				if(components[i] instanceof JScrollPane){
					JScrollPane scrollPane = (JScrollPane) components[i];
					scrollPane.getViewport().getView().setEnabled(false);
				}
			}
		}
		else{
			panel.setEnabled(true);
			Component[] components = panel.getComponents();
			for(int i = 0; i < components.length; i++){
				components[i].setEnabled(true);
				if(components[i] instanceof JScrollPane){
					JScrollPane scrollPane = (JScrollPane) components[i];
					scrollPane.getViewport().getView().setEnabled(true);
				}
			}
		}
	}
	
	public static boolean validatePanelFields(JPanel panel){
		boolean valid = true;
		
		Component[] components = panel.getComponents();
		for(int i = 0; i < components.length; i++){
			if(components[i] instanceof JTextField){
				JTextField field = (JTextField)components[i];
				if(field.getText().isEmpty() || field.getText().replace(" ", "").equals("") || field.getText() == null){
					valid = false;
					field.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
				}
				else{
					field.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
				}
			}
			if(components[i] instanceof JScrollPane){
				JScrollPane scrollPane = (JScrollPane)components[i];
				if(scrollPane.getViewport().getComponents()[0] instanceof JTextArea){
					JTextArea field = (JTextArea)scrollPane.getViewport().getComponents()[0];
					if(field.getText().isEmpty() || field.getText().replace(" ", "").equals("") || field.getText() == null){
						valid = false;
						scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
					}
					else{
						scrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
					}
				}
			}
		}
		
		if(!valid)
			Main.frame.displayMessage(false, "Error(s) found in some fields.\nPlease check through fields highlighted in red before trying again.");
		
		return valid;
	}

}
